﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using System.Reflection;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Shop.MemberCard
{
    public partial class MemberCardTrade : System.Web.UI.Page
    {
        public WxMemberCardRight cardRight { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            ZLCMS.Business.DwzFrame.FrameHelper _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
            ZLCMS.Business.DwzFrame.ReturnCode rc;
            string _type=null;
            if (!string.IsNullOrEmpty(ViewState["type"] as string))
            {
                _type = ViewState["type"] as string;
            }
            if (!IsPostBack)
            {
                //设置对象
                ViewState["type"] = Request["type"];
                
                int uid = 0;
                if (int.TryParse(Request["uid"], out uid))
                {
                    using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        cardRight = session.Get<WxMemberCardRight>(uid);
                        if (cardRight != null)
                        {
                            //保存uid
                            ViewState["uid"] = uid;
                         
                        }
                    }
                }
            }
            else
            {
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (ViewState["uid"] == null || ViewState["uid"] == "")
                    {
                        cardRight = new WxMemberCardRight();
                    }
                    else
                    {
                        cardRight = session.Get<WxMemberCardRight>(ViewState["uid"]);
                    }
                    if (string.IsNullOrEmpty(txt_consumption.Text) || (decimal.Parse(txt_consumption.Text) > cardRight.RemainAmount && _type == "comsumption"))
                    {
                        rc = new ZLCMS.Business.DwzFrame.ReturnCode()
                        {
                            statusCode = 300,
                            message = "参数错误"
                        };

                        rc.callbackType = "forward";
                        rc.forwardUrl = "/SystemManage/Basics/Shop/MemberCard/MemberCardTrade.aspx?tabid=MemberCardTrade";

                        if (Request["submit_op"] != "1")
                        {
                            rc.forwardUrl += "&uid=" + cardRight.Id;
                        }

                        _framePage.Alert(rc);
                        return;
                    }
                    else
                    {

                        //记录日志
                        MemberCardLog log = new MemberCardLog()
                        {
                            MemberAccount = cardRight.MemberAccount,
                            Amount = decimal.Parse(txt_consumption.Text),
                            Remark = txt_remark.Value,
                            OwnerAccount = "admin",
                            OptTime = DateTime.Now,
                          
                        };
                        if (_type == "charge")
                        {
                            log.OptType = "charge";
                            cardRight.RemainAmount = cardRight.RemainAmount + decimal.Parse(txt_consumption.Text);
                        }
                        else
                        {
                            log.OptType = "consumption";
                            cardRight.RemainAmount = cardRight.RemainAmount - decimal.Parse(txt_consumption.Text);
                        }
                        log.Balance = cardRight.RemainAmount.Value;
                        session.Save(log);
                    }

                    session.SaveOrUpdate(cardRight);
                    session.Flush();
                }
            

            //框架返回成功提示
            rc = new ZLCMS.Business.DwzFrame.ReturnCode()
           {
               statusCode = 200,
               message = "操作成功"
           };
            rc.callbackType = "closeCurrent";


            _framePage.Alert(rc);
            }
        }
    }
}